package com.epfresh.views;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.CountDownTime;
import com.epfresh.utils.TimeStringUtils;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-5-27
 * @Description
 */

public class HomeCountDownView extends RelativeLayout {

    private TextView tvTitle;
    private TextView tvHourH;
    private TextView tvHourL;
    private TextView tvMinutesH;
    private TextView tvMinutesL;
    private TextView tvSecondH;
    private TextView tvSecondL;
    private TextView tvNumFlagH;
    private TextView tvNumFlagL;
    private CountDownTimer timer;
    private CountDownTime countDownTime;
    long millis;
    public HomeCountDownView(Context context) {
        super(context);
    }

    public HomeCountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeCountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View child = View.inflate(getContext(), R.layout.layout_count_down_home, null);

        tvTitle = (TextView) child.findViewById(R.id.tv_title);
        tvHourH = (TextView) child.findViewById(R.id.tv_num_hour_0);
        tvHourL = (TextView) child.findViewById(R.id.tv_num_hour_1);
        tvMinutesH = (TextView) child.findViewById(R.id.tv_num_minute_0);
        tvMinutesL = (TextView) child.findViewById(R.id.tv_num_minute_1);
        tvSecondH = (TextView) child.findViewById(R.id.tv_num_second_0);
        tvSecondL = (TextView) child.findViewById(R.id.tv_num_second_1);
        tvNumFlagH = (TextView) child.findViewById(R.id.tv_num_0);
        tvNumFlagL = (TextView) child.findViewById(R.id.tv_num_1);


        addView(child);
    }

    private void updateStatus(String time){
        //Log.e("cdt.getCountDownTime()","time:"+time);
        if (time != null && time.length() == 8){
            //Log.e("cdt.getCountDownTime()","time2:"+time);
            String hourH = time.substring(0, 1);
            String hourL = time.substring(1, 2);

            String minutesH = time.substring(3, 4);
            String minutesL = time.substring(4, 5);

            String secondH = time.substring(6, 7);
            String secondL = time.substring(7, 8);

            //Log.e("cdt.getCountDownTime()","hourH:"+hourH);
            //Log.e("cdt.getCountDownTime()","hourL:"+hourL);
            //Log.e("cdt.getCountDownTime()","minutesH:"+minutesH);
            //Log.e("cdt.getCountDownTime()","minutesL:"+minutesL);
            //Log.e("cdt.getCountDownTime()","secondH:"+secondH);
            //Log.e("cdt.getCountDownTime()","secondL:"+secondL);
            tvHourH.setText(hourH);
            tvHourL.setText(hourL);
            //Log.e("cdt.getCountDownTime()","tvHourH.:"+tvHourL.getText());

            tvMinutesH.setText(minutesH);
            tvMinutesL.setText(minutesL);
            tvSecondH.setText(secondH);
            tvSecondL.setText(secondL);

        }


    }

    public void showTimer(CountDownTime cdt,long millisInFuture, long countDownInterval) {
        this.millis = millisInFuture;
        millisInFuture = Math.abs(millisInFuture);
        Log.e("cdt.getCountDownTime()","millis:"+millis);
        this.countDownTime = cdt;
        if (timer != null) {
            timer.cancel();
        }

        timer = new CountDownTimer(millisInFuture, countDownInterval) {

            @Override
            public void onTick(long millisUntilFinished) {

                if (onCountDownListener != null){
                    onCountDownListener.onCountDownListener(false);
                }

                //Log.e("cdt.getCountDownTime()","millisUntilFinished:"+millisUntilFinished);
                if (countDownTime != null) {
                    //Log.e("cdt.getCountDownTime()","millisUntilFinished2:"+millisUntilFinished);
                    String timeLongToString = TimeStringUtils.getTimeLongToString(millisUntilFinished);
                    //Log.e("cdt.getCountDownTime()","timeLongToString:"+timeLongToString);
                    updateStatus(timeLongToString);

                    if (millis > 0) {
                        tvTitle.setText("距结束");
                    } else if (millis < 0) {
                        tvTitle.setText("距开始");
                    }
//                    long totalTime = countDownTime.getTotalTime();

                }

            }

            @Override
            public void onFinish() {
                updateStatus("00:00:00");

                if (onCountDownListener != null){
                    onCountDownListener.onCountDownListener(true);
                }

            }
        };
        timer.start();
    }

    public void finishTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
            updateStatus("00:00:00");
        }
    }


    public HomeCountDownView setTitleSize(float textSize) {
        tvTitle.setTextSize(textSize);
        return this;
    }

    public HomeCountDownView setTitleColor(int color) {
        tvTitle.setTextColor(color);
        return this;
    }

    public HomeCountDownView setTimeBody(int color) {
        tvTitle.setTextColor(color);
        tvHourH.setTextColor(color);
        tvHourL.setTextColor(color);
        tvMinutesH.setTextColor(color);
        tvMinutesL.setTextColor(color);
        tvSecondH.setTextColor(color);
        tvSecondL.setTextColor(color);
        return this;
    }

    public HomeCountDownView setTimeSize(float textSize) {
        tvTitle.setTextSize(textSize);
        tvHourH.setTextSize(textSize);
        tvHourL.setTextSize(textSize);
        tvMinutesH.setTextSize(textSize);
        tvMinutesL.setTextSize(textSize);
        tvSecondH.setTextSize(textSize);
        tvSecondL.setTextSize(textSize);
        return this;
    }

    public HomeCountDownView setTimeIntervalSize(float textSize) {
        tvNumFlagH.setTextSize(textSize);
        tvNumFlagL.setTextSize(textSize);
        return this;
    }

    public HomeCountDownView setTimeIntervalColor(int color) {
        tvNumFlagH.setTextColor(color);
        tvNumFlagL.setTextColor(color);
        return this;
    }

    OnCountDownListener onCountDownListener;

    public OnCountDownListener getOnCountDownListener() {
        return onCountDownListener;
    }

    public void setOnCountDownListener(OnCountDownListener onCountDownListener) {
        this.onCountDownListener = onCountDownListener;
    }

    public interface OnCountDownListener{
        public void onCountDownListener(boolean isFinish);
    }
}
